//
//  AXFShoppingCartController.m
//  爱鲜蜂-购物车
//
//  Created by Joker on 2016/11/20.
//  Copyright © 2016年 new moon. All rights reserved.
//

#import "AXFShoppingCartController.h"
#import "Masonry.h"
#import "AXFDizhiCell.h"
#import "AXFFootView.h"
#import "AXFDetailsModel.h"
#import "AXFModelManager.h"
#import "AXFEmptyShoppingCartView.h"
#import "AXFGoodsCell.h"
#import "AXFTabBarController.h"
#import "AXFHomeController.h"
#import "AXFPayViewController.h"


static NSString * firstCellID = @"firstCellID";
static NSString * goodsCellID = @"goodsCellID";
@interface AXFShoppingCartController () <UITableViewDelegate, UITableViewDataSource, AXFGoodsCell, AXFEmptyShoppingCartView,AXFFootViewDelegate>

@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) AXFModelManager *modelManager;
@property (nonatomic,strong) NSMutableArray *managerModelList;
@property (nonatomic,weak) AXFFootView *footView;
@property (nonatomic,weak) AXFEmptyShoppingCartView *emptyShoppingView;


@end

@implementation AXFShoppingCartController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshMyList:) name:@"refreshList" object:nil];
}

- (void)refreshMyList:(NSNotification *)no
{
    [self.tableView reloadData];                                         
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    AXFModelManager *modelManager = [AXFModelManager shareModelManager];
    self.modelManager = modelManager;
    self.managerModelList = modelManager.managerModelList;
    [self.tableView reloadData];
    self.navigationController.navigationBar.hidden = NO;
}


#pragma mark 1.搭建界面
- (void)setupUI{
    
    
    AXFEmptyShoppingCartView *emptyShoppingView = [[AXFEmptyShoppingCartView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.emptyShoppingView = emptyShoppingView;
    emptyShoppingView.delegate = self;
    
    
    //创建控件
    UITableView * TV = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    self.tableView = TV;
    
    [self.view addSubview:TV];
    
    
    //设置数据源和代理
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:emptyShoppingView];
    
    //注册
    [self.tableView registerClass:[AXFDizhiCell class] forCellReuseIdentifier:firstCellID];
    [self.tableView registerNib:[UINib nibWithNibName:@"AXFGoodsCell" bundle:nil] forCellReuseIdentifier:goodsCellID];
    //设置约束
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
}
- (void)jumpToMainHomeController:(UIButton *)sender
{
    self.tabBarController.selectedIndex = 0;
    
}

#pragma mark -footView的代理方法
- (void)pushToPayController
{
    AXFPayViewController *payViewController = [[AXFPayViewController alloc] init];
    
    [self.navigationController pushViewController:payViewController animated:YES];
}

- (void)AXFFootView:(AXFFootView *)footView withAllRightBtn:(UIButton *)btn{
    
    NSMutableArray * cellArray = [NSMutableArray arrayWithCapacity:10];
    [self.tableView.visibleCells enumerateObjectsUsingBlock:^(__kindof UITableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [cellArray addObject:obj];
    }];
    
    [cellArray removeObjectAtIndex:0];
    
    [cellArray enumerateObjectsUsingBlock:^(AXFGoodsCell*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj changeRightBtn:btn];
    }];
    
}

#pragma mark 2.数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            if (self.managerModelList.count != 0) {
                self.emptyShoppingView.hidden = YES;
            }else{
                self.emptyShoppingView.hidden = NO;
            }
            return self.managerModelList.count;
            break;
        default:
            break;
    }
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:firstCellID forIndexPath:indexPath];
        //小箭头
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    
    
    AXFGoodsCell * cell = [tableView dequeueReusableCellWithIdentifier:goodsCellID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.managerModelList[indexPath.row];
    
    [AXFFootView shareFootView].model = self.managerModelList;
    
    cell.delegate = self;
    
    
    
    return cell;
}

#pragma mark - ZXFGoodsCell 的代理方法
//  MARK: 1.删除
- (void)deleteGoodsCellModel:(AXFDetailsModel *)model
{
    [self.managerModelList removeObject:model];
    [AXFFootView shareFootView].model = self.managerModelList;
    
    [self.tableView reloadData];
}
//  MARK: 2.减号
- (void)minusesGoodsCellModel
{
    [AXFFootView shareFootView].model = self.managerModelList;
    AXFModelManager *managerModel = [AXFModelManager shareModelManager];
    __block NSInteger sum = 0;
    [managerModel.managerModelList enumerateObjectsUsingBlock:^(AXFDetailsModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        sum += obj.shopCount;
    }];
    
    AXFTabBarController *tab = (AXFTabBarController *)self.tabBarController;
    if (sum != 0) {
        tab.childViewControllers[2].tabBarItem.badgeValue = [NSString stringWithFormat:@"%zd", sum];
    }else{
        
        tab.childViewControllers[2].tabBarItem.badgeValue = nil;
        
    }
    
}
//  MARK: 3.加号
- (void)addGoodsCellModel
{
    [AXFFootView shareFootView].model = self.managerModelList;
    AXFModelManager *managerModel = [AXFModelManager shareModelManager];
    __block NSInteger sum = 0;
    [managerModel.managerModelList enumerateObjectsUsingBlock:^(AXFDetailsModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        sum += obj.shopCount;
    }];
    
    AXFTabBarController *tab = (AXFTabBarController *)self.tabBarController;
    if (sum != 0) {
        tab.childViewControllers[2].tabBarItem.badgeValue = [NSString stringWithFormat:@"%zd", sum];
    }else{
        
        tab.childViewControllers[2].tabBarItem.badgeValue = nil;
        
    }
    
    
}

//  MARK: 4.对号
- (void)AXFGoodsCell:(UITableViewCell *)cell withRightBtn:(UIButton *)btn{
    [AXFFootView shareFootView].model = self.managerModelList;
    
    //遍历数组中的Model的选中状态
    __block int count = 0;
    [self.managerModelList enumerateObjectsUsingBlock:^(AXFDetailsModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.btnSelect) {
            count++;
        }
    }];
    if (count == self.managerModelList.count) {
        self.modelManager.btnArrayselect = YES;
    }else{
        self.modelManager.btnArrayselect = NO;
    }
    [self.footView changeRightBtnWithManger:self.modelManager];
    
}

//设置头尾view的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 100;
    }
    
    return 10;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return 100;
    }
    
    return 20;
}
//设置Cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return 50;
    }
    
    return 80;
    
}

//设置加载头尾视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 1) {
        return [[[NSBundle mainBundle] loadNibNamed:@"AXFHeadView" owner:nil options:nil] lastObject];
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section == 1) {
        AXFFootView *footView = [AXFFootView shareFootView];
        self.footView = footView;
        footView.delegate = self;
        return footView;
    }
    return nil;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
